<template>
<div class="ivu-card ivu-card-bordered ivu-card-dis-hover">
  <div class="ivu-card-head"><p>{{ $route.meta.title }}</p></div>
  <div class="ivu-card-body">
    <iForm ref="formRef" :model="formModel" :rules="formRules" :label-width="100">

      <FormItem prop="title" label="标题：">
        <Input v-model="formModel.title" placeholder="名称"/>
      </FormItem>

      <FormItem prop="content" label="内容：">
        <Input v-model="formModel.content" type="textarea" placeholder="内容"/>
      </FormItem>

      <FormItem prop="status" label="是否可用：">
        <i-select v-model="formModel.status">
          <i-option v-for="(v, k) in apiConfig['common_status']" :value="k.toString()" :key="k">{{ v }}</i-option>
        </i-select>
      </FormItem>

      <FormItem prop="is_home" label="首页是否显示">
        <i-select v-model="formModel.is_home">
          <i-option v-for="(v, k) in apiConfig['notice_is_home']" :value="k.toString()" :key="k">{{ v }}</i-option>
        </i-select>
      </FormItem>

      <FormItem>
        <Button type="primary" :loading="submitLoading" @click="onClickSubmit">创建</Button>
        <Button type="ghost" @click="$router.go(-1);">返回</Button>
      </FormItem>
    </iForm>
  </div>
</div>
</template>

<script>
import qs from 'qs';
import { Form, Input, Button, Select, Option, Message } from 'iview';
import apiConfig from '@/utils/apiConfig';
import auth from '@/utils/auth';
import ajaxiView from '@/utils/ajaxiView';

export default {
  components: {
    [Form.name]: Form,
    [Form.Item.name]: Form.Item,
    [Input.name]: Input,
    [Button.name]: Button,
    [Select.name]: Select,
    [Option.name]: Option
  },
  data() {
    return {
      apiConfig,
      token: auth.getToken(),
      productsClass: {},
      formModel: {
        title: '',
        content: '',
        status: '0',
        is_home: '0'
      },
      submitLoading: false,
      formRules: {
        title: [
          {type:'string', required: true, message: '请输入标题!', trigger: 'blur'}
        ],
        content: [
          {type:'string', required: true, message: '请输入内容!', trigger: 'blur'}
        ],
        status: [
          {type: 'string', required: true, message: '请选择是否可用!', trigger: 'blur'}
        ],
        is_home: [
          {type: 'string', required: true, message: '请选择首页是否显示!', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    onClickSubmit() {
      var _this = this;
      _this.$refs.formRef.validate((valid) => {
        _this.submitLoading = true;

        if(valid) {
          ajaxiView.post('/backstage/notice?token='+_this.token, qs.stringify(_this.formModel)).then(function(data) {
            if(data !== false) {
              Message.success({
                content: data.msg,
                onClose: function() {
                  _this.$router.push({ name: 'systemNoticeIndex' });
                }
              });
            }

            _this.submitLoading = false;
          });
        } else {
          _this.submitLoading = false;
        }
      });
    },
    _initialize() {
      var _this = this;
      apiConfig.getItem().then(function(api_config) {
        _this.apiConfig = api_config;
      });
    }
  },
  mounted() {
    this._initialize();
  }
}
</script>

<style>
</style>